package com.ds.plugins.action;

import com.ds.annotation.Aggregation;
import com.ds.annotation.AggregationType;
import com.ds.annotation.UserSpace;
import com.ds.common.JDSException;
import com.ds.common.logging.ChromeProxy;
import com.ds.config.ResultModel;
import com.ds.context.JDSActionContext;
import com.ds.esd.annotation.CustomAnnotation;
import com.ds.esd.annotation.field.APIEventAnnotation;
import com.ds.esd.annotation.ui.RequestPathEnum;
import com.ds.esd.engine.ESDFacrory;
import com.ds.web.RemoteConnectionManager;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping(value = {"/action/build/public/"})
@Aggregation(type=AggregationType.MENU,userSpace = UserSpace.SYS)
public class PublicAction {
    @RequestMapping(value = {"exportLocalServer"}, method = {RequestMethod.POST})
    @CustomAnnotation(index = 1, caption = "本地发布", imageClass = "spafont spa-icon-jumpto")
    @APIEventAnnotation(customRequestData = {RequestPathEnum.SPA_PROJECTNAME})
    @ResponseBody
    public ResultModel<Boolean> exportLocalServer(String projectName, String serverId) {
        ResultModel resultModel = new ResultModel();
        ChromeProxy chrome = getCurrChromeDriver();
        if (projectName != null) {
            String finalLocalServerId = serverId;
            RemoteConnectionManager.getConntctionService(projectName).execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        ESDFacrory.getAdminESDClient().exportLocalServer(projectName, finalLocalServerId, chrome);
                    } catch (JDSException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
        return resultModel;
    }

    @RequestMapping(value = {"exportRemoteServer"}, method = {RequestMethod.POST})
    @CustomAnnotation(index = 2, caption = "远程发布", imageClass = "spafont spa-icon-indent")
    @APIEventAnnotation(customRequestData = {RequestPathEnum.SPA_PROJECTNAME})
    @ResponseBody
    public ResultModel<Boolean> exportRemoteServer(String projectName, String serverId) {
        ResultModel resultModel = new ResultModel();
        ChromeProxy chrome = getCurrChromeDriver();
        String remoteServerId = serverId.toString();
        if (projectName != null) {
            RemoteConnectionManager.getConntctionService(projectName.toString()).execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        ESDFacrory.getAdminESDClient().exportRemoteServer(projectName, remoteServerId, chrome);
                    } catch (JDSException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
        return resultModel;
    }


    public ChromeProxy getCurrChromeDriver() {
        ChromeProxy chrome  = JDSActionContext.getActionContext().Par(ChromeProxy.class);
        return chrome;
    }


}
